<?php
header( 'Content-Type: application/json');
session_start();
require_once('../../lib/JSON.php');
require_once('../../config/connection.php');
require_once('../../utils/CommonUtils.php');
require_once('../../utils/DBUtils.php');
require_once('../../utils/Resources.php');
require_once('../../application/model/BaseDAO.php');
require_once("../../lib/core/Session.php");
require_once("../../lib/core/Object.php");
$commonUtils = new CommonUtils();
$baseDAO = new BaseDAO();
$returnRes = array();
$success=true;

$page = $commonUtils->getParameter("page");
if (!isset($page) || $page == '') {
	$page = "list";
}

$action = $commonUtils->getParameter("crudaction");
$sortDirection = $commonUtils->getParameter("dir", "DESC");

$id = $commonUtils->getParameter("id");
$name = $commonUtils->getParameter("name");
$address = $commonUtils->getParameter("address");
$phone = $commonUtils->getParameter("phone");
$fax= $commonUtils->getParameter("fax");
$numregister= $commonUtils->getParameter("numregister");
$note= $commonUtils->getParameter("note");
$createddate = $commonUtils->getDate();
$updateddate = $commonUtils->getDate();
$createdby = Session::get('userid');
$updatedby = Session::get('userid');

if ($page == "list") {
	$sortExpression = $commonUtils->getParameter("sort", "id");
	$begin = $commonUtils->getParameter("pg4_offset", 0);
	$limit = $commonUtils->getParameter("maxPageItems", 10);
	
	// xóa record
	if (isset($action) && $action  == 'delete') {
		$checkList = $commonUtils->getParameter("checkList");
		if (isset($action) && $action  == 'delete') {
			$checkList = $commonUtils->getParameter("checkList");
			$ids = explode("|", $checkList);
			$returnRes['ids'] = $checkList;
			if (isset($ids) && is_array($ids)) {
				foreach($ids as $id) {
					$baseDAO->del("provider", $id);
				}	
			}
		}
	}
	
	$query = " 1 = 1";
	if (isset($name) && $name != '') {
		$query .= " AND name LIKE '%" . $name . "%'";
	}
	if (isset($phone) && $phone!= '') {
		$query .= " AND phone LIKE '%" . $phone. "%'";
	}
	if (isset($numregister) && $numregister!= '') {
		$query .= " AND numregister LIKE '%" . $numregister . "%'";
	}
	if (isset($sortExpression) && $sortExpression != '') {
		$orderby = " ORDER BY $sortExpression $sortDirection";
	}
	
	$items = $baseDAO->getItems("provider", array("id", "name", "address", "phone", "fax", "numregister", "note",  "updateddate", "updatedby"), $query, $orderby, $begin, $limit);
	$totalItems = $baseDAO->getTotalItems("provider", $query);
	$returnRes['listResult'] = $items;
	$returnRes['totalItems'] = $totalItems;
	$returnRes['data'] = array('crudaction' => 'search');
	
}else if($page == 'edit'){
	$returnRes["messageResponse"] = $action;
	if (isset($action) && $action == 'insert-update') {
		if (isset($id) && $id > 0) {
			$values = array($id, $name, $address, $phone, $fax, $numregister, $note, $updateddate, $updatedby);
			$rs=$baseDAO->checkDuplicate("provider", '', $name, $id);
			if($rs>0){	
				$success= false;			
				$returnRes["messageResponse"] = $Resources["providerdublicate"];
			}else{
				$res = $baseDAO->update("provider", array("id", "name","address","phone","fax","numregister","note", "updateddate", "updatedby"), $values);
				if (isset($res) && $res >= 1) {
					$returnRes["messageResponse"] = $Resources["updatesuccessful"];
				}else{
					$returnRes["messageResponse"] = $Resources["updatefailure"] ;
				}
			}
		}else{
			$values = array($name, $address, $phone, $fax, $numregister, $note, $createddate, $updateddate, $createdby, $updatedby);
			$rs=$baseDAO->checkDuplicate("provider",'',$name,0);
			if($rs>0){	
				$success= false;
				$returnRes["messageResponse"] = $Resources["providerdublicate"];
				
			}else{
				$res = $baseDAO->create("provider", array("name", "address", "phone", "fax", "numregister", "note", "createddate", "updateddate", "createdby", "updatedby"), $values);
				if (isset($res) && $res >= 1) {
					$returnRes["messageResponse"] =$Resources["addsuccessful"] ;
				}else{
					$returnRes["messageResponse"] = $Resources["addfailure"];
				}	
			}
		}
	}
	$item = $baseDAO->getItem("provider",array("id", "name","address","phone","fax","numregister","note", "updateddate",  "updatedby"), $id);
	$returnRes['data'] = $item;
}

$returnRes['success'] = true;
$jsonUtil = new ext_Json();
echo $jsonUtil->encode($returnRes);

?>